package com.eastrobot.report.util;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;

public class ParamUtil {

	public static Object setParam(Class<?> clz, HttpServletRequest req) {
		Map<String, String[]> params = req.getParameterMap();
		Set<String> keys = params.keySet();
		Object o = null;
		try {
			o = clz.newInstance();
			for (String key : keys) {

				String[] vv = params.get(key);
				// 如果元素的值的长度大于1，表示获取的应该是一个数组
				if (vv.length > 1) {
					BeanUtils.copyProperty(o, key, vv);
				} else {
					BeanUtils.copyProperty(o, key, vv[0]);
				}

			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return o;
	}

}
